﻿
@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService


<MDialog @bind-Value="Dialog"
         Width="600">
    <MDCard>
        <MCardTitle Class="text-h5">
            添加微博动态
        </MCardTitle>
        <MCardText>
            填写微博链接后点击保存，再到列表中找到创建的动态，点击编辑，在新的窗口中确认无误后发布
        </MCardText>
        <MCardText>
            <MTextField @bind-Value="Model.Link" Label="微博链接"  />
        </MCardText>



        <MCardActions Class="pb-4 pe-6 ps-6 pt-0">
            <MSpacer></MSpacer>
            <CnGalWebSite.Components.Buttons.MasaButton Text="保存" IsAsync="true" OnClick="OnSave" Icon="mdi-check" Color="success" Rounded />
        </MCardActions>
    </MDCard>
</MDialog>
@code {
    [Parameter]
    public EventCallback OnRefreshTable { get; set; }

    public AddWeiboNewsModel Model { get; set; } = new AddWeiboNewsModel();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    bool Dialog { get; set; }

    public async Task OnSave()
    {
        //上传
        try
        {
            //处理数据
            long temp = 0;
            if (string.IsNullOrWhiteSpace(Model.Link))
            {
                await PopupService.ToastErrorAsync("添加微博动态失败", "微博Id无效");
            }


            var obj = await _httpService.PostAsync<AddWeiboNewsModel, Result>("api/news/AddWeiboNews", Model);
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("添加微博动态失败", obj.Error);
            }
            else
            {

                await PopupService.ToastSuccessAsync("添加微博动态成功", "添加微博动态成功");
                await OnRefreshTable.InvokeAsync();
                Dialog = false;
            }
        }
        catch (Exception ex)
        {
            await PopupService.ToastErrorAsync("添加微博动态失败", "添加微博动态失败");
            //await ErrorHandler.ProcessError(ex, "保存动态数据失败");
        }
    }
    public void Refresh()
    {
        Dialog = true;
        Model = new AddWeiboNewsModel();

        StateHasChanged();
    }
}
